package bootstrap.liftweb

import net.liftweb._
import util._
import Helpers._

import common._
import http._
import js.jquery.JQueryArtifacts
import sitemap._
import Loc._
import mapper._

import code.model._
import net.liftmodules.JQueryModule
import code.model._
import code.model.util._


class Boot {
  def boot {
    if (!DB.jndiJdbcConnAvailable_?) {
      val vendor =
        new StandardDBVendor(Props.get("db.driver") openOr "org.h2.Driver",
          Props.get("db.url") openOr
            "jdbc:h2:lift_proto.db;AUTO_SERVER=TRUE",
          Props.get("db.user"), Props.get("db.password"))
      LiftRules.unloadHooks.append(vendor.closeAllConnections_! _)
      DB.defineConnectionManager(DefaultConnectionIdentifier, vendor)
    }
    Schemifier.schemify(true, Schemifier.infoF _)
    LiftRules.addToPackages("code")
    def sitemap = SiteMap(
      Menu.i("Home") / "index" >> User.AddUserMenusAfter,      
      //Menu.i("Door") / "index" >> TemplateBox(() => Templates( "doorcontrol" :: Nil)),
      Menu.i("Chat") / "chat" >> User.AddUserMenusAfter,
      Menu(Loc("Static", Link(List("static"), true, "/static/index"),
        "Static Content")))

    LiftRules.setSiteMap(sitemap)

    LiftRules.ajaxStart = Full(() => LiftRules.jsArtifacts.show("ajax-loader").cmd)
    LiftRules.ajaxEnd = Full(() => LiftRules.jsArtifacts.hide("ajax-loader").cmd)

    LiftRules.early.append(_.setCharacterEncoding("UTF-8"))

    LiftRules.htmlProperties.default.set((r: Req) =>
      new Html5Properties(r.userAgent))

    def setupUserWithCasState(cs:CASStateData):Unit = {
      if (cs.authenticated){
        val authcate = cs.username
        Globals.casState(cs)
        Globals.currentUser(authcate)
      }
    }
    //CASAuthentication.attachCASAuthenticator(new CASAuthenticator("specialConsideration", () => Globals.casState.authenticated, setupUserWithCasState _))

    S.addAround(DB.buildLoanWrapper)
    
    
    ArduinoBridge ! InitPort("/dev/tty.usbmodemfd121")
  }
}
